###   Implementation Constraints on natural climate solutions
###   Author: Kroeger, Timm; Erbaugh, James; Luo, Zhixian; Hilary Brumberg; Waverly Eichhorst; Margaret Hegwood; Anna LoPresti; Priya Shyamsundar; Peter W. Ellis; Lauren E. Oakes; Dow Martin; Pedro H. S. Brancalion; Mieke Bourne; Arundhati Jagadish; Kemen G. Austin; Andrew Kinzer; Marcos Sanjuán; Lisa McCullough; Marta Echavarria,

library(tidyverse)
library(ggplot2)
library(grid)
library(ggpattern)

nbase<-read.csv("NBaseData_v1.csv")

## Fig. S9. Subregional distribution of constraint observations by NCS pathway

pathway_plot_region<-nbase %>%
  group_by(Subregion, NCS_Pathway)%>%
  tally(.)

totals_pathway_region<-ggplot(pathway_plot_region %>% filter(Subregion!="NA" & NCS_Pathway!="NA" & NCS_Pathway!="other (please specify)"), aes(x=fct_reorder(Subregion, n, .fun = sum), y=n, fill=NCS_Pathway))+
  geom_col() +
  coord_flip()+
  theme_classic()+
  ylab(NULL)+xlab(NULL)+ labs(fill="")+
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.text = element_text(""))+
  scale_fill_manual(values = c("agroforestry" = "#d1bbd7", 
                               "avoided coastal wetland conversion" = "#ae76a3",
                               "avoided forest conversion" = "#882e72",
                               "avoided grassland conversion" = "#5289c7",
                               "avoided peatland conversion" = "#7bafde",
                               "climate-smart forestry" = "#4eb265",
                               "coastal wetland restoration" = "#90c987",
                               "grassland restoration" = "#cae0ab",
                               "peatland restoration" = "#f7f056",
                               "reduced woodfuel harvest in forests" = "#f6c141",
                               "reforestation" = "#f1932d",
                               "regenerative agriculture (other than agroforestry)" = "#e8601c",
                               "savanna management" = "#dc050c"
  ))
totals_pathway_region

ggsave("Fig S9.jpg", totals_pathway_region,  width=8, height=4, dpi=720)